package com.rimi.lzhg.controller;

import com.rimi.lzhg.entry.Menu;
import com.rimi.lzhg.response.ResponseEntity;
import com.rimi.lzhg.service.MenuService;
import com.rimi.lzhg.service.TodayMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private TodayMenuService todayMenuService;

    @ApiOperation(value = "添加菜单信息",notes = "采用post提交方式添加菜单信息")
    @PostMapping("/manager/menu")
    public Object saveMenuManager(Menu menu){
        Menu menuByName = menuService.getMenuByName(menu.getName());
        if(menuByName!=null){
            return ResponseEntity.error("菜单名称已经被使用");
        }
        boolean b = menuService.saveMenu(menu);
        if (b){
            return ResponseEntity.success("菜品添加成功");
        }
        return ResponseEntity.error("添加菜品失败，请重试");
    }

    @ApiOperation(value = "查询菜单列表",notes = "根据查询条件查询菜单的信息,如不添加则查询所有")
    @GetMapping("/manager/menu")
    public Object getMenuList(Menu menu){
        List<Menu> userList = menuService.getUserList(menu);
        return ResponseEntity.success(userList.size(),userList);
    }

    @ApiOperation(value = "修改菜单信息",notes = "采用put提交方式根据Id修改菜单信息")
    @PutMapping("/manager/menu")
    public Object updateMenuManager(Menu menu){
        Menu menuByName = menuService.getMenuByName(menu.getName());
        if(menuByName!=null){
            return ResponseEntity.error("菜单名称已经被使用");
        }
        boolean b = menuService.updateMenu(menu);
        if(b){
            return ResponseEntity.success("修改菜单信息成功");
        }
        return ResponseEntity.error("服务器修改菜单信息失败！！！");
    }

    @ApiOperation(value = "状态删除菜单信息",notes = "采用delete提交方式根据id删除菜单信息")
    @DeleteMapping("/manager/menu")
    public Object delteMenuByIdManager(Integer id){
        boolean isok = todayMenuService.getTodayMenuByMenuId(id);
        if(isok){
            return ResponseEntity.error("该菜品今日已被使用,无法删除");
        }
        //验证该菜单是否在今日菜单中被使用 通过菜品ID和今日日期查询今日菜单 如果存在则无法删除
        boolean b = menuService.deleteMenuById(id);
        if(b){
            return ResponseEntity.success("菜单删除成功");
        }
        return ResponseEntity.error("菜单删除失败");
    }



    @ApiOperation(value = "查询菜单列表",notes = "根据查询条件查询菜单的信息,如不添加则查询所有")
    @GetMapping("/menu")
    public Object getMenuInfo(Menu menu){
        return menuService.getUserList(menu);
    }

    @ApiOperation(value = "根据编号查询菜单",notes = "使用url形式传递ID查询菜单信息 例如: /menu/10")
    @GetMapping("/menu/{id}")
    public Object getMenuById(@PathVariable Integer id){
        return  menuService.getUserById(id);
    }

    @ApiOperation(value = "添加菜单信息",notes = "采用post提交方式添加菜单信息")
    @PostMapping("/menu")
    public Object saveMenu(Menu menu){
        return menuService.saveMenu(menu);
    }

    @ApiOperation(value = "修改菜单信息",notes = "采用put提交方式根据Id修改菜单信息")
    @PutMapping("/menu")
    public Object updateMenu(Menu menu){
        return menuService.updateMenu(menu);
    }

    @ApiOperation(value = "删除菜单信息",notes = "采用delete提交方式根据id删除菜单信息")
    @DeleteMapping("/menu")
    public Object delteMenuById(Integer id){
        return menuService.deleteMenuById(id);
    }


}
